#!/bin/bash

show_help() {
  echo -e "gnocchi - metric service"
  echo -e "Usage:"
  echo -e "$title [options] [status|console|update|vm <UUID> <metric>|gnocchi native params...] [-- <gnocchi options...>]"
  echo -e "Options:"
  echo -e "\t--auth-url           keystone auth url"
  echo -e "\t--project            project name"
  echo -e "\t--user | -u          user name"
  echo -e "\t--password | -p      password"
  echo -e "\t--help | -h          show this help and exit"
  echo -e "Parameters:"
  echo -e "\tstatus                    report gnocchi status"
  echo -e "\tupdate                    refresh the cached metric list"
  echo -e "\tconsole                   gnocchi interactive mode"
  echo -e "\tvm <UUID> <metric> [-f]   get vm metric data"
  echo -e "\t                          metric: ALL|cpu|mem|net|disk|gpu"
  echo -e "\t                          \"-f\" refresh the cached metric list"
  echo -e "Examples:"
  echo -e "\t$ $title status"
  echo -e "\t$ $title vm 4b6c0529-2471-4f8c-be20-a455b0e7c792 cpu --granularity 300 --start \"2021-03-04T14:50:00+08:00\" --stop \"2021-03-04T15:55:00+08:00\""
  echo -e "\t$ $title vm 3c2e6ee8-2554-4a88-9805-6c3cf5e476c0 ALL --start now-30 --granularity 5"
}

cmdopts=$(getopt --longoptions project:,user:,password:,auth-url:,title:,help \
                     --options +hu:p: -- "$@")

# set positional parameters
eval set -- "$cmdopts"

title=do_gnocchi
project=$OS_PROJECT_NAME
username=$OS_USERNAME
password=$OS_PASSWORD
auth_url=$OS_AUTH_URL
op=
interactive=false

while true; do
  case "$1" in
    --title )
        title="$2"
        shift 2;;
    -h | --help )
        show_help
        exit ;;
    --project )
        project="$2"
        shift 2;;
    -u | --user )
        username="$2"
        shift 2;;
    -p | --password )
        password="$2"
        shift 2;;
    --auth-url )
        auth_url="$2"
        shift 2;;
    -- ) shift; break ;;
    * ) break ;;
  esac
done

while true; do
  case "$1" in
    status )
        op="$1"
        shift;;
    update )
        op="$1"
        shift;;
    console)
        op="$1"
        interactive=true
        shift;;
    vm )
        op=$1
        vmuuid=$2
        metric=$3
        [[ -z "$vmuuid" || -z "$metric" ]] && { show_help; exit; }
        shift 3;;
    -- ) shift; break ;;
    * ) break ;;
  esac
done

str0="--os-project-domain-name $OS_PROJECT_DOMAIN_NAME \
 --os-user-domain-name $OS_USER_DOMAIN_NAME \
 --os-project-name $project \
 --os-username $username \
 --os-password $password \
 --os-auth-url $auth_url \
 --os-interface $OS_INTERFACE \
 --os-region-name $OS_REGION_NAME \
 --os-auth-type $OS_AUTH_PLUGIN"

if $interactive; then
    str1="sudo ssh -t root@$controller docker exec -uroot -it gnocchi_api"
else
    str1="sudo ssh root@$controller docker exec -uroot gnocchi_api"
fi

if [ -z "$op" ]; then  # gnocchi native parameters
    [ $# -eq 0 ] && { show_help; exit; }
    str3=
    while true; do
      case "$1" in
        -- ) shift; break ;;
        * ) 
            [ -z "$1" ] && break
            str3="$str3 $1"
            shift
            ;;
      esac
    done
    $str1 gnocchi $str0 $* $str3
    exit
fi

if [ "$op" == "console" ]; then
    $str1 gnocchi $str0 $*
    exit
elif [ "$op" == "status" ]; then
    $str1 gnocchi $str0 $* status
    exit
elif [ "$op" == "update" ]; then
    $str1 /bin/bash /nyyos/remote/test-gnocchi.sh update $str0 $*
    exit
fi

# op=="vm"

$str1 \
  /bin/bash /nyyos/remote/test-gnocchi.sh \
  vm $vmuuid $metric \
  $str0 $*

